#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

void SetNonBlock(int fd){
  int fl = fcntl(fd, F_GETFL);
  if(fl < 0){
    perror("fcntl\n");
    return;
  }
  fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}


int main(){
  SetNonBlock(0);
  while(1){
    char buffer[1024];
    ssize_t s = read(0, buffer, sizeof(buffer) - 1);
    if(s > 0){
      buffer[s] = 0;
      write(1, buffer, strlen(buffer));
      printf("read success, s: %d, errno: %d\n", s, errno);
    }
    else{
      if(errno == EAGAIN || errno == EWOULDBLOCK){
        printf("数据未就绪\n");
        printf("read failed, s: %d, errno: %d\n", s, errno);
        sleep(1);
        continue;
      }
    }
  } 
  return 0;
}
